package com.alex.business.controller.admin;

import com.alex.business.config.BusinessApplication;
import com.alex.server.dto.ChapterDto;
import com.alex.server.dto.ChapterPageDto;
import com.alex.server.dto.PageDto;
import com.alex.server.dto.ResponseDto;
import com.alex.server.service.IChapterService;
import com.alex.server.util.ValidatorUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/chapter")
public class ChapterController {
    private static final Logger LOG = LoggerFactory.getLogger(BusinessApplication.class);
    public static final String BUSINESS_NAME = "大章";

    @Autowired
    private IChapterService chapterService;

    /**
     * 查询
     * @param chapterPageDto
     */
    @PostMapping("list")
    public ResponseDto list(@RequestBody ChapterPageDto chapterPageDto) {

        ResponseDto responseDto = new ResponseDto();
        ValidatorUtil.require(chapterPageDto.getCourseId(), "课程ID");
        chapterService.pageList(chapterPageDto);
        responseDto.setContent(chapterPageDto);
        return responseDto;
    }

    /**
     * 保存更新
     * @param chapterDto
     */
    @PostMapping("saveCourse")
    public ResponseDto addCourse(@RequestBody ChapterDto chapterDto) {
        // 保存校验
        ValidatorUtil.require(chapterDto.getName(), "名称");
        ValidatorUtil.require(chapterDto.getCourseId(), "课程ID");
        ValidatorUtil.length(chapterDto.getCourseId(), "课程ID", 1, 8);

        ResponseDto responseDto = new ResponseDto();
        chapterService.addCourse(chapterDto);
        responseDto.setContent(chapterDto);
        return responseDto;
    }

    /**
     * 删除
     * @param id
     */
    @DeleteMapping("delete/{id}")
    public ResponseDto addCourse(@PathVariable String id) {
        LOG.info("id{}", id);
        ResponseDto responseDto = new ResponseDto();
        chapterService.delete(id);
        return responseDto;
    }
}
